package com.ai.async;

import com.ai.common.constant.HttpConstant;
import com.ai.common.constant.enumP.AlgorithmStateEnums;
import com.ai.common.constant.enumP.AvailableEnums;
import com.ai.entity.alarm.SwitchAlgorithmParam;
import com.ai.entity.function.FunctionPO;
import com.ai.entity.made.AlgorithmServerRelation;
import com.ai.entity.server.ServerPO;
import com.ai.service.made.FunctionService;
import com.ai.service.made.IAlgorithmServerRelationService;
import com.ai.service.made.ServerService;
import com.ai.util.GetCameraInfoHk;
import com.ai.util.HttpRequestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @Author jiaok
 * @Date 2024-12-30 17:20
 */
public class BatchGetAlgorithmStatusAsync {

    public static ExecutorService executorService = Executors.newFixedThreadPool(20);

    /**
     * 获取摄像头状态任务
     */
    public static class GetAlgorithmStatus implements Callable<Integer> {

        /**
         * 功能id
         */
        private Integer functionId;

        /**
         * 算法服务器关系处理
         */
        private IAlgorithmServerRelationService iAlgorithmServerRelationService;

        public GetAlgorithmStatus(Integer functionId, IAlgorithmServerRelationService iAlgorithmServerRelationService ) {
            this.functionId = functionId;
            this.iAlgorithmServerRelationService = iAlgorithmServerRelationService;
        }

        @Override
        public Integer call() throws Exception {
            List<AlgorithmServerRelation> deployRelationList = iAlgorithmServerRelationService.getDeployRelationList(String.valueOf(functionId));
            long count = deployRelationList.stream().filter(relation -> AvailableEnums.ENABLE.getType().equals(relation.getAlgorithmState())).count();
            //可能单个算法运行在多个服务器上，但是只要有一个正在运行，就认为该算法正常运行，返回1，进行统计累加即可
            return count > 0 ? 1:0;
        }
    }

}
